﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using GameLib.Utils;
using GameLib.WorldMap;

namespace Sandbox.Scene
{
	public class MapCursor : MapSceneObject
	{
		public MapCursor()
		{
		}

		public Point Location
		{
			get
			{
				return m_Location;
			}
			set
			{
				// TODO: better not update anything if location didn't change
				m_Location = value;

				//Point topLeft = Scene.GetTileTopLeft((int)m_Location.X, (int)m_Location.Y);
				Point center = Scene.GetTileCenter((int)m_Location.X, (int)m_Location.Y);
				m_CursorImage.SetValue(Canvas.LeftProperty, center.X - m_CursorImage.Width / 2);
				m_CursorImage.SetValue(Canvas.TopProperty, center.Y - m_CursorImage.Height / 2);
			}
		}

		public int Radius
		{
			get { return _Radius; }
			set
			{
				if (_Radius != value)
				{
					_Radius = value;
					BuildCursorImage();
				}
			}
		}
		private int _Radius = 1;

		protected override void Display()
		{
			if (m_CursorImage == null)
			{
				m_CursorImage = new Image();
				m_CursorImage.SetValue(Canvas.ZIndexProperty, 5);
				BuildCursorImage();
			}

			SceneCanvas.Children.Add(m_CursorImage);
		}

		protected override void Disappear()
		{
			SceneCanvas.Children.Remove(m_CursorImage);
		}

		protected override Canvas SceneCanvas
		{
			get { return Scene.GetSceneObjectCanvas(MapSceneObjectLayers.AboveTerrain); }
		}

		private void BuildCursorImage()
		{
			if (m_CursorImage == null)
				return;

			int diameter = 1 + 2 * (Radius - 1);
			int imgWidth = Scene.GetHorizontalSpan(diameter);
			int imgHeight = Scene.GetVerticalSpan(diameter);
			GameVector topCoord = Map.ToHexCoordinate(Radius - 1, 0);
			int cenY = topCoord.Y + (Radius - 1) * 2;
			Point topLeft = Scene.GetTileTopLeft(topCoord.X, topCoord.Y);

			Rect imgRect = new Rect(0, topLeft.Y, imgWidth, imgHeight);
			DrawingGroup drawGroup = new DrawingGroup();
			drawGroup.ClipGeometry = new RectangleGeometry(imgRect);

			GeometryDrawing rect = new GeometryDrawing(
				new SolidColorBrush(Colors.Transparent),
				null,
				new RectangleGeometry(imgRect));

			drawGroup.Children.Add(rect);

			BitmapImage bitmap = new BitmapImage(new Uri("../../Content/Images/Map/Elements/map_cursor.png", UriKind.Relative));

			// TODO: we should use Map.RasterizeCircle here
			for (int x = 0; x < diameter; x ++)
			{
				int offset = Math.Abs(x - (Radius - 1));
				int halfHeight = ((diameter - offset) * 2 - 1) / 2;

				for (int y = cenY - halfHeight; y <= cenY + halfHeight; y += 2)
				{
					ImageDrawing imgDrawing = new ImageDrawing();
					imgDrawing.ImageSource = bitmap;
					Point tileTopLeft = Scene.GetTileTopLeft(x, y);
					imgDrawing.Rect = new System.Windows.Rect(tileTopLeft.X, tileTopLeft.Y, bitmap.PixelWidth, bitmap.PixelHeight);
					drawGroup.Children.Add(imgDrawing);
				}
			}
			DrawingImage drawingImgSource = new DrawingImage(drawGroup);
			drawingImgSource.Freeze();
			m_CursorImage.Source = drawingImgSource;
			m_CursorImage.Width = drawingImgSource.Width;
			m_CursorImage.Height = drawingImgSource.Height;
		}

		private Image m_CursorImage;
		private Point m_Location;
	}
}
